const {apiPath} = require('../../config');
const {get,post,success,failure} = require('../../common/request');
const {html_upload,file_ground_del,addressSep} = require("./file");

const content = {
    /** 查找所有模块名 **/
    find_module_name: (req, res) => {                                   //查询已有模块
        let url = `${apiPath}/app/get-app-model-name`;
        let options = {
            data: req.query,
            success: result => success(result, res),
            failure: error => failure(error, res)
        };
        get(url, options);
    },
    /** 根据模块名查找数据 **/
    get_dynamic_find: (req,res) => {                                    //根据模块查询此模块下所有的数据
        let url = `${apiPath}/app/get-dynamic-find`;
        let options = {
            data: req.query,
            success: result => success(result, res),
            failure: error => failure(error, res)
        };
        get(url, options);
    },
    /** 更新数据 **/
    update_dynamic_find: (req,res) => {
        let url= `${apiPath}/cms/update-dynamic-find`;
        let data = req.body;
        if(typeof data["obj.ContentUrl"] != "undefined") {
            data["obj.ContentUrl"] = html_upload(addressSep(data["obj.ContentUrl"]),data["obj.content"]);
            if(data["obj.ContentUrl"] == "error") {
                res.json({
                    status : false,
                    error : "html文件创建失败"
                })
            }
        }
        let options = {
            data: data,
            success: result => success("更新成功!!!", res),
            failure: error => failure("更新失败!!!", res)
        };
        post(url,options);
    },
    /** 删除数据 **/
    del_dynamic_find : (req,res) => {                                    //删除数据
        let url = `${apiPath}/cms/del-dynamic-find`;
        let {id,contentUrl} = req.body;
        let photoUrl = req.body["photoUrl[]"];
        file_ground_del(photoUrl);
        file_ground_del(contentUrl);
        let options = {
            data: {id:id},
            success: result => success("删除成功!!!", res),
            failure: error => failure("删除失败!!!", res)
        };
        post(url,options);
    },
    /** 创建数据 **/
    create_dynamic_find : (req,res) => {
        let url= `${apiPath}/cms/create-dynamic-find`;
        var data = req.body;
        if(data["obj.content"] != "") {
            data["obj.ContentUrl"] = html_upload(addressSep(data["obj.ContentUrl"]),data["obj.content"]);
        }
        if(data["obj.ContentUrl"] == "error") {
            res.json({
                status : false,
                error : "html文件创建失败"
            })
        }
        let options = {
            data: data,
            success: result => success("创建成功!!!", res),
            failure: error => failure("创建失败!!!", res)
        };
        post(url,options);
    },
    /** 根据id获取数据**/
    get_dynamic_find_by_id : (req,res) => {
        let url = `${apiPath}/cms/get-dynamic-find-by-id`;
        let options = {
            data: req.query,
            success: result => success(result, res),
            failure: error => failure(error, res)
        };
        get(url, options);
    }

};
module.exports = content;
